package com.b2b2c.store.single.logic;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.data.redis.core.RedisTemplate;

import com.b2b2c.conf.MybatisSingleConf;
import com.b2b2c.store.context.MySessionContext;
import com.b2b2c.store.single.entity.StoreAuthGroupAccess;
import com.b2b2c.store.single.service.IBaseService;
import com.b2b2c.store.single.service.IValidateService;

/**
 * 单库抽象类
 * 
 * @author Administrator
 *
 */
public abstract class AbstractLogic<T, V> implements IValidateService, IBaseService<T>{

	public HttpServletRequest getReq() {
		return req;
	}

	private static SqlSession sqlTemp;

	private HttpServletRequest req;

	private String splitKey;
	
	
	private List<V> data;
	
	/**
	 * 参数
	 */
	protected Map<String, String> args;


	public List<V> getData() {
		return data;
	}

	public void setData(List<V> data) {
		this.data = data;
	}


	static {
		MybatisSingleConf conf = new MybatisSingleConf();

		sqlTemp = new SqlSessionTemplate(conf.sqlSessionFactory(conf.singleDbDataSource()));
	}

	public SqlSession getSqlTemp() {
		return sqlTemp;
	}

	/**
	 * @param args
	 */
	public AbstractLogic(Map<String, String> args) {
		this.args = args;
	}
	
	/**
	 * @param args
	 */
	public AbstractLogic(HttpServletRequest req) {
		this.req = req;
	}
	
	public AbstractLogic(List<V> data, HttpServletRequest req) {
		this.req = req;
		this.data = data;
	}

	
	/**
	 * @param args
	 */
	public  AbstractLogic(List<V> data, String splitKey) {
		this.splitKey = splitKey;
		
		this.data = data;
	}
	
	public AbstractLogic(List<V> data) {
		this.data = data;
	}

	public Map<String, String> getMap() {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();

		Enumeration<String> param = this.req.getParameterNames();
		while (param.hasMoreElements()) {
			String paramName = param.nextElement();

			String paramValue = this.req.getParameter(paramName);
			// 形成键值对应的map
			map.put(paramName, paramValue);
		}
		
		return map;
	}
	
	
	/**
	 * 获取主键编号字符串
	 */
	public  String getIdString(String method)
	{
		
		if (data == null || data.isEmpty()) {
			return "";
		}
		
		int i = data.size();
		
		int j ;
		
		V obj;
		
		StringBuilder str = new StringBuilder("");
		
		String key;
		
		try {
			for (j = 0; j < i ; j ++) {
				
				obj = data.get(j);
				
				key = obj.getClass().getMethod(method).invoke(obj).toString();
				
				str.append(",").append(key);
			}
		} catch (Exception e) {
			// TODO: handle exception
//			e.printStackTrace();
			return "";
		}
		
		return str.substring(1);
	}
	
	
	public List<T> getDataList()
	{
		return null;
	}
	
}
